import { getChinaChats } from '@/api/map'

import { getTreeData } from '@/utils/tree'

const getDefaultState = () => {
  return {}
}

const state = getDefaultState()

const mutations = {}

const actions = {
  // 获取中国行政区划数据
  async getChinaDistrict() {
    return new Promise(async (resolve, reject) => {
      // getChinaDistrict()
      //   .then((response) => {
      //     resolve(response)
      //   })
      //   .catch((err) => {
      //     reject(err)
      //   })
      try {
        const response = await fetch('/api/fileService/file/download?id=1887072568991600642', { encoding: 'utf-8' })

        if (!response.ok) {
          reject(response)
        }

        const data = await response.json()
        // 处理解析后的 JSON 数据
        resolve(getTreeData(data.districts[0].districts, 'districts'))
      } catch (error) {
        reject(error)
      }
    })
  },
  // 获取中国地图数据
  async getChinaChats() {
    return new Promise(async (resolve, reject) => {
      getChinaChats()
        .then((response) => {
          resolve(response)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
